Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Introduction to Array in Python
Python - Array Methods
Quiz
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Python - Booleans

Booleans in Python are simple yet powerful. They can hold two values: True and False. These values enable Python to evaluate conditions or comparisons, playing a critical role in control flow and decision-making processes in programming.

Understanding Booleans

In Python, a Boolean value can either be True or False. These values are often the result of comparison operations but can also be used directly for controlling the flow of programs with conditional statements.

Example

This example illustrates how to assign and print Boolean values in Python.

Python3

. . . .

Explanation:

  • is_active = True and is_registered = False demonstrate assigning Boolean values to variables.
  • The print statements display the Boolean status of each variable, confirming is_active as True and is_registered as False.

Falsy Boolean Values

In Python, most objects are considered True when evaluated in a Boolean context. However, certain "falsy" values are considered False. These include:

  • None
  • False
  • Zero of any numeric type, for example, 0, 0.0, 0j
  • Any empty sequence, for example, '', (), []
  • Any empty mapping, for example, {}

Example

This example evaluates a set of values that are "falsy," or evaluated as False in a Boolean context.

Python3

. . . .

Explanation:

  • Evaluating None, False, 0, an empty string '', an empty list [], and an empty dictionary {} with the bool() function demonstrates that these values all return False.

Using Booleans in Control Structures

Booleans are crucial for controlling the flow of programs through conditional statements like if and else.

Example

This example uses a Boolean variable to dictate which branch of an if statement is executed.

Python3

. . . .

Explanation:

  • has_access = True sets the Boolean variable has_access to True.
  • if has_access: checks if has_access is True, and executes the corresponding block to print "Access granted." If it were False, "Access denied" would be printed instead.

Booleans are fundamental in Python for making decisions within the program, enabling logical operations that depend on the truth or falsity of conditions. This makes programs adaptable to various scenarios based on inputs and conditions.

Python - Numbers
Python - Type Conversions
Mark as Completed

On this page

Understanding Booleans

Example

Falsy Boolean Values

Example

Using Booleans in Control Structures

Example